/***************其它工具方法*****************/


/**
 *  得到事件
 */

function getEvent() {
    if (window.event) {
        return window.event;
    }
    func = getEvent.caller;
    while (func != null) {
        var arg0 = func.arguments[0];
        if (arg0) {
            if ((arg0.constructor == Event || arg0.constructor == MouseEvent ||
                    arg0.constructor == KeyboardEvent) ||
                (typeof(arg0) == "object" && arg0.preventDefault &&
                    arg0.stopPropagation)) {
                return arg0;
            }
        }
        func = func.caller;
    }
    return null;
}

/**
 *  阻止事件冒泡
 */
function stopDefault() {
    var e = getEvent();
    if (window.event) {
        //e.returnValue=false;//阻止自身行为
        e.cancelBubble = true; //阻止冒泡
    } else if (e.preventDefault) {
        //e.preventDefault();//阻止自身行为
        e.stopPropagation(); //阻止冒泡
    }
}

/**
 * 获取URL中的请求参数

 getUrlParam('http://www.baidu.com/?aa=1&bb=1234567890')

 * @method getUrlParam
 * @param {String} Url 可选,带参数的URL, 默认为当前访问的URL
 * @return {Object}  {"aa":"1","bb":"1234567890"}
 *
 */

function getUrlParam(Url) {
    var result = new Object();
    if (Url) {
        var tmp = Url.split("?")[1];
        if (tmp) {
            tmp = tmp.split("&");
            for (var j = 0; j < tmp.length; j++) {
                result[tmp[j].split("=")[0]] = tmp[j].split("=")[1];
            }
        }
    } else {
        var tmp = location.search.substring(1);
        if (tmp) {
            tmp = tmp.split("&");
            for (var j = 0; j < tmp.length; j++) {
                result[tmp[j].split("=")[0]] = tmp[j].split("=")[1];
            }
        }
    }
    return result;
}


/**
 * 将对象序列化成字符串
 * @param obj
 * @returns {string}
 */
function serialize(obj) {
    var str = '';
    if (isEmpty(obj)) {
        return str;
    }
    for (var key in obj) {
        var val = obj[key];

        if (!isArray(val)) {
            str += '&' + key + '=' + val;
        } else {
            for (var i = 0, len = val.length; i < len; i++) {
                str += '&' + key + '=' + val[i];
            }
        }
    }
    return str.substring(1);
}

/**
 * 将字符串反序列化成对象
 * @param obj
 * @returns {string}
 */
function unSerialize(str) {
    var result = {};
    if (isEmpty(str)) {
        return result;
    }
    var params = str.split("&");

    if (isEmpty(params) || params.length === 0) {
        return result;
    }

    for (var i = 0, len = params.length; i < len; i++) {
        var param = params[i];
        var arr = param.split("=");

        var key = arr[0];
        var val = arr[1];

        if (isEmpty(key) || isEmpty(val)) {
            continue;
        }
        var old = result[key];
        if (isEmpty(old)) {
            result[key] = val;
            continue;
        }

        if (!isArray(old)) {
            result[key] = [old, val];
            continue;
        } else {
            old.push(val);
            continue;
        }
    }
    return result;
}

/**
 * 复制文本到粘贴板
 */
function clipboardData(msg) {
    if ($$.browser.isIE) {
        window.clipboardData.clearData();
        window.clipboardData.setData("Text", msg);
        alert("已经成功复制到剪帖板上！");
    } else if (isFirefox) {
        alert("浏览器不支持复制文本到剪帖板");
    }
}

function forbidRight() {
    /*
     * $(document).bind("contextmenu",function(e){ return false; });
     */
    document.oncontextmenu = function () {
        event.returnValue = false;
    };
}

function getParameter(key, loc) {
    var arr = [];
    loc = loc || 0;
    var patt = new RegExp(name + '=([^&]*)', "g");
    var str = location.search.substring(1);

    while ((result = patt.exec(str)) !== null) {
        arr.push(result[1]);
    }

    if (arr.length === 0) {
        return '';
    }
    if (isNotEmpty(loc)) {
        return arr[loc];
    }
    return arr.join(",");
}

function uuid() {
    var b = [], c = "0123456789ABCDEF".split("");
    for (var a = 0; a < 36; a++) {
        b[a] = Math.floor(Math.random() * 16);
    }
    b[14] = 4;
    b[19] = (b[19] & 3) | 8;

    for (var i = 0; i < 36; i++) {
        b[i] = c[b[i]];
    }
    b[8] = b[13] = b[18] = b[23] = "-";
    return b.join("");
}

function guid() {
    var str = uuid();
    str = str.replaceAll("-", "");
    return str;
}

function logger(msg) {
    var d = new Date();
    var time = formatDate(d);
    var info = time + " : " + d.getMilliseconds() + "  " + msg;

    console.info(info);
}
